<template>
  <div v-if="!loading2">
    <div v-if="error.show">
      <van-empty :description="error.message"></van-empty>
    </div>
    <div v-if="!loading && !error.show" class="order_wrapper">
      <van-nav-bar
        class="nav_bar"
        fixed
        title="确认订单"
        @click-left="onClickLeft"
      />
      <div class="store">
        <!-- <div class="tips">距离最近</div> -->
        <div class="top">
          <div class="name van-ellipsis">
            {{ storeInfo.title }}
          </div>
          <div class="rate">★ 5.0</div>
        </div>
        <!-- <div class="count">99999+次服务</div> -->
        <div class="bottom">
          <div class="location">
            <div class="address_name">
              <van-icon
                name="location-o"
                color="#2e2f30"
                class="location_icon"
              />
              {{ storeInfo.address }}
            </div>
          </div>
          <div class="meter">{{ storeInfo.distance }}</div>
        </div>
      </div>
      <div v-if="Object.keys(carInfo).length > 0" class="order_info">
        <van-cell title="服务车辆" :border="false">
          <template>
            <div class="info_value">
              {{ carInfo.plateNumber }} {{ carInfo.brand }}
            </div>
          </template>
        </van-cell>
        <van-cell title="车主联系方式" :border="false">
          <template>
            <div class="info_value">{{ carInfo.ownerPhone }}</div>
          </template>
        </van-cell>
      </div>
      <div class="order_info">
        <div class="order_title">已选项目</div>
        <div class="order_item">
          <template>
            <van-collapse v-model="activeName" :border="false">
              <van-collapse-item
                v-for="(v, i) in skuListInfo[0].skuList"
                :key="i"
                :title="v.goodsName"
                :name="i"
                :border="false"
              >
                <template #right-icon>
                  <div class="item_price">￥{{ v.totalPrice }}</div>
                </template>
                <template>
                  <div class="goods_item">
                    <div class="left">
                      <van-image class="serve_pic" :src="v.pic" />
                      <div class="left_title">
                        {{ v.simpleSpecs.trim() }}
                      </div>
                    </div>
                    <div class="right">
                      <!-- <div class="price">￥{{ v.price }}</div> -->
                      <div class="num">x{{ v.num }}</div>
                    </div>
                  </div>
                  <!-- <div class="serve_item">
                <van-cell v-for="item in 2" :key="item" center :border="false">
                  <template #title>
                    <span>服务</span>
                    <span class="serve_title">更换机油滤芯</span>
                  </template>
                  <template>
                    <div class="serve_info">
                      <span class="serve_info_price">￥39.9</span>
                      <span class="serve_info_num">x1</span>
                    </div>
                  </template>
                </van-cell>
              </div> -->
                </template>
              </van-collapse-item>
              <!-- <van-collapse-item title="小保养" :name="3" :border="false">
            <template #right-icon>
              <div class="item_price">￥899</div>
            </template>
          </van-collapse-item> -->
            </van-collapse>
          </template>
        </div>
      </div>
      <div v-if="!userData.isVip" class="vip">
        <div class="vip_top">
          <div class="vip_top_left">
            <van-image
              class="vip_icon"
              :src="require('@/assets/icon/serve/vip.png')"
            />
            <span class="vip_tips">安心修大会员</span>
          </div>
          <div class="vip_top_right">
            <div class="btn" @click="goVip">立即开卡</div>
            <!-- <div class="price">￥199/年</div> -->
          </div>
        </div>
        <!-- <div class="vip_tip">
          <div class="vip_tip_text">
            开通会员立享折扣，预计本单可再省
            <span class="vip_tip_text_price">199.99</span>
            元
          </div>
        </div> -->
      </div>
      <div class="order_info">
        <div v-if="userData.isVip" class="dec">
          <img src="@/assets/icon/store/vip.png" alt="" />
          <p>安心修大会员</p>
        </div>
        <div class="info-box">
          <van-cell center :border="false">
            <template #title>
              <div class="info_value">
                订单总价
                <span class="margin_left"
                  >共{{ skuListInfo[0].skuList.length }}项服务</span
                >
              </div>
            </template>
            <template>
              <div class="info_value red">￥{{ priceInfo.goodsPrice }}</div>
            </template>
          </van-cell>
          <!-- <van-cell center :border="false">
            <template #title>
              <div class="info_value">工时费</div>
            </template>
            <template>
              <div class="info_value red">￥25</div>
            </template>
          </van-cell> -->
          <van-cell
            center
            :is-link="Object.keys(currentCouponse).length == 0"
            :border="false"
            class="cell_spec"
          >
            <template #title>
              <div class="info_value">优惠券</div>
            </template>
            <template>
              <div class="info_value red" @click="showPopup">
                {{
                  Object.keys(this.currentCouponse).length > 0
                    ? `${currentCouponse.couponName} -${currentCouponse.price}`
                    : canUseCoupons.length > 0
                    ? `当前有${canUseCoupons.length}张优惠券可以使用`
                    : "暂无可用优惠券"
                }}
              </div>
            </template>
          </van-cell>
          <!-- <van-cell v-if="userData.isVip" center :border="false">
            <template #title>
              <div class="info_value">安心修会员专属折扣(工时费8折)</div>
            </template>
            <template>
              <div class="info_value red">-¥6.99</div>
            </template>
          </van-cell> -->
          <van-cell center :border="false">
            <template #title>
              <div class="info_value">合计</div>
            </template>
            <template>
              <div class="info_value red">￥{{ orderInfo.price }}</div>
            </template>
          </van-cell>
        </div>
      </div>
      <div class="order_info">
        <van-cell center :border="false">
          <template #title>
            <div class="info_value">发票</div>
          </template>
          <template>
            <div class="info_value">请在到点服务时向门店索取</div>
          </template>
        </van-cell>
      </div>
      <div class="order_bottom" @click="goToPay">
        <div class="pay_btn">去支付</div>
      </div>
      <van-dialog v-model="show" show-cancel-button>
        <div
          style="display: flex; justify-content: center; padding: 80px 0 80px 0"
        >
          <QRCode v-if="paySrc" ref="qr" :url="paySrc"></QRCode>
        </div>
      </van-dialog>
    </div>

    <van-popup
      v-model="showCouponList"
      round
      position="bottom"
      style="height: 80%; padding-top: 4px"
    >
      <van-tabs v-model="active" color="#447ecc" @change="tabChange">
        <van-tab title="可用"></van-tab>
        <van-tab title="不可用"></van-tab>
      </van-tabs>
      <div style="padding: 20px">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text=""
          @load="onLoad"
        >
          <OrderCoupon
            v-for="v in list"
            :key="v.id"
            :item="v"
            :selectId="currentCouponse.id"
            :type="active"
            @updteSelectCoupon="updateCurrentCoupon"
          ></OrderCoupon>
        </van-list>
      </div>

      <!-- <div v-if="active == 0" class="button" @click="useCoupon">使用</div> -->
    </van-popup>
  </div>
</template>

<script>
import api from "@/api/api";
import { isZxcxAPP } from "@/utils/zxcxWebkits";
import { mapGetters } from "vuex";
import QRCode from "@/components/qrCode.vue";
import OrderCoupon from "@/components/order_coupon.vue";

export default {
  components: { QRCode, OrderCoupon },
  props: {},
  data() {
    return {
      active: 0,
      loading: false,
      loading2: true,
      finished: true,
      couponList: [],
      showCouponList: false,
      activeName: [],
      query: {},
      orderInfo: {},
      skuListInfo: {},
      storeInfo: {},
      carInfo: {},
      paySrc: "",
      show: false,
      error: {
        show: false,
        message: "",
      },
      list: [],
      canUseCoupons: [],
      cantUseCoupons: [],
      currentCouponse: {},
      currentCouponId: "",
      priceInfo: {},
      initOpenCoupon: true,
    };
  },
  watch: {},
  computed: {
    ...mapGetters(["carList", "currentCarIndex", "userData"]),
  },
  methods: {
    onClickLeft() {
      this.$router.back();
    },
    tabChange() {
      if (this.active == 0) {
        this.list = this.canUseCoupons;
      } else {
        this.list = this.cantUseCoupons;
      }
    },
    goVip() {
      this.$router.push({
        path: "/vip",
      });
    },
    async goToPay() {
      this.$toast.loading({
        message: "加载中",
      });
      let res = await api.createTrade({
        settleId: this.orderInfo.settleId,
      });
      let res2 = await api
        .pay({
          sn: res.sn,
        })
        .catch((err) => {
          console.log(err);
        });
      this.$toast.clear();
      if (res2) {
        if (isZxcxAPP()) {
          window.location.href = res2;
        } else {
          this.show = true;
          this.paySrc = res2;
        }
      } else {
        this.$toast.fail({
          message: "支付失败",
        });
      }
    },
    async getBuyDetail() {
      this.$toast.loading({
        forbidClick: true,
        duration: 0,
      });
      this.orderInfo = await api
        .buyNow(this.query)
        .catch((err) => {
          this.error.show = true;
          this.error.message = err;
        })
        .finally(() => {
          this.loading = false;
        });
      this.skuListInfo = this.orderInfo.skuInfoList;
      this.priceInfo = this.orderInfo.priceDetailDTO;
      this.canUseCoupons = this.orderInfo.canUseCoupons;
      this.cantUseCoupons = this.orderInfo.cantUseCoupons;
      this.list = this.canUseCoupons;
      if (this.canUseCoupons.length > 0) {
        this.currentCouponse = this.canUseCoupons[0];
        this.currentCouponId = this.currentCouponse.id;
      }
      this.loading2 = false;
      this.storeInfo = await api.getStoreDetail({
        shopId: this.orderInfo.skuInfoList[0].shop.shopId,
        // latitude: "31.026962",
        // longitude: "119.911212",
      });
      this.$toast.clear();
    },
    showPopup() {
      this.showCouponList = true;
    },
    onLoad() {
      // for (let i = 0; i < 10; i++) {
      //   this.couponList.push(this.list.length + 1);
      // }
      // // 加载状态结束
      // this.loading = false;
      // // 数据全部加载完成
      // if (this.couponList.length >= 0) {
      //   this.finished = true;
      // }
    },
    useCoupon() {
      this.showCouponList = false;
    },
    async updateCurrentCoupon(idList) {
      if (this.canUseCoupons.length == 0) return;
      if (this.active == 1) return;
      if (idList.length == 0) {
        this.currentCouponse = {};
      } else {
        this.currentCouponse = this.canUseCoupons.find(
          (item) => item.id == idList[0]
        );
        this.currentCouponId = this.currentCouponse.id;
      }

      this.$toast.loading({
        message: "加载中...",
        duration: 0,
      });
      //调用选择优惠券接口
      this.orderInfo = await api
        .select_coupon({
          memberCouponId: this.currentCouponId,
          settleId: this.orderInfo.settleId,
          used: Object.keys(this.currentCouponse).length > 0,
        })
        .finally(() => {
          this.$toast.clear();
        });
      this.skuListInfo = this.orderInfo.skuInfoList;
      this.priceInfo = this.orderInfo.priceDetailDTO;
      this.canUseCoupons = this.orderInfo.canUseCoupons;
      this.cantUseCoupons = this.orderInfo.cantUseCoupons;
      this.list = this.canUseCoupons;
      if (Object.keys(this.currentCouponse).length == 0) {
        this.currentCouponId = "";
      }
      if (!this.initOpenCoupon) {
        this.showCouponList = false;
      }
      this.initOpenCoupon = false;
      // if (this.canUseCoupons.length > 0) {
      //   this.currentCouponse = this.canUseCoupons[0];
      //   this.currentCouponId = this.currentCouponse.id;
      // }
    },
  },
  async created() {
    if (this.carList.length) {
      this.carInfo = this.carList[this.currentCarIndex];
    }
    if (this.$route.query.cartType) {
      //购物车
    } else {
      this.query.goodsId = this.$route.query.id;
      this.query.num = Number(this.$route.query.num);
      this.query.skuId = this.$route.query.skuId;
      this.query.promotionId = this.$route.query.promotionId;
      this.query.promotionType = Number(this.$route.query.promotionType);
      await this.getBuyDetail();
    }
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
.order_wrapper {
  min-height: 100vh;
  background: url("https://zxcxappimg.oss-cn-hangzhou.aliyuncs.com//zxcxmall/2025/05/29/44dea588672142d48f873df92c7b625e.jpg")
    no-repeat;
  background-color: #fff;
  background-size: contain;
  background-attachment: fixed;
  background-position: top;
  box-sizing: border-box;
  padding: 130px 50px 150px 50px;
  position: relative;

  .nav_bar {
    background: url("https://zxcxappimg.oss-cn-hangzhou.aliyuncs.com//zxcxmall/2025/05/29/44dea588672142d48f873df92c7b625e.jpg")
      no-repeat;
    background-size: cover;
    background-position: top center;

    /deep/ .van-nav-bar__text {
      color: #fff;
      margin-left: 20px;
      font-size: 52px;
    }

    /deep/ .van-icon {
      font-size: 50px;
      font-weight: bold;
      color: #fff;
    }

    /deep/ .van-nav-bar__title {
      color: #fff;
    }
  }

  /deep/ .van-hairline--bottom::after {
    border: unset;
  }

  .store {
    margin-top: 30px;
    padding: 60px 30px;
    background: #fff;
    border-radius: 20px;
    position: relative;

    .tips {
      padding: 15px 27px;
      background: linear-gradient(to bottom, #00bdf9, #015fea);
      border-top-left-radius: 20px;
      border-bottom-right-radius: 20px;
      position: absolute;
      left: 0;
      top: 0;
      font-size: 34px;
      color: #fff;
    }

    .top {
      display: flex;
      align-items: flex-start;
      margin-top: 10px;
      justify-content: space-between;
      padding: 10px 0;

      .name {
        font-size: 46px;
        color: #333;
        // max-width: 80%;
        font-weight: bold;
        flex: 10;
      }

      .rate {
        font-size: 46px;
        color: #c10500;
        margin-left: 20px;
        flex: 2;
      }
    }

    .count {
      margin: 25px 0;
      font-size: 40px;
      color: #878787;
    }

    .bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .location {
        flex: 3;

        .address_name {
          font-size: 38px;
          margin-left: 15px;

          .location_icon {
            font-size: 50px;
          }
        }
      }

      .meter {
        font-size: 38px;
        margin-left: 20px;
      }
    }
  }

  .order_info {
    position: relative;
    padding: 45px 30px 45px 30px;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    margin-top: 40px;
    border-radius: 20px;

    /deep/ .van-cell {
      padding: 10px 0;
    }

    .cell_spec {
      /deep/ .van-cell__value {
        flex: 3;
      }
    }

    .dec {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      left: 0;
      background: linear-gradient(to top, #6c6c6b, #353334);
      padding: 10px 15px;
      border-radius: 16px 0 16px 0;

      img {
        width: 70px;
        object-fit: contain;
      }

      p {
        color: #ecc085;
        font-size: 38px;
        margin: 0 10px;
      }
    }

    .info-box {
      margin-top: 50px;
    }

    .info_value {
      font-size: 40px;
      color: #333333;
      white-space: nowrap;
      flex: 1;
    }
  }

  .order_title {
    font-size: 46px;
    color: #323233;
  }

  .order_item {
    margin-top: 20px;
    margin-bottom: 25px;

    /deep/ .van-cell {
      background: #f8faff;
      padding: 20px 30px;
    }

    /deep/ .van-collapse-item {
      margin-bottom: 30px;
    }

    .item_price {
      font-size: 46px;
      color: #c10500;
      font-weight: bold;
    }

    /deep/ .van-collapse-item__content {
      padding: 30px 0;
    }

    .goods_item {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      margin-bottom: 30px;

      .left {
        display: flex;
        align-items: flex-start;
        flex: 5;

        .serve_pic {
          flex: 1;
          width: 190px;
          min-width: 60px;
          aspect-ratio: 1;
        }

        &_title {
          flex: 3;
          font-size: 44px;
          color: #000000;
          // max-width: 180px;
          margin-left: 20px;
        }
      }

      .right {
        text-align: right;
        flex: 1;

        .price {
          font-size: 40px;
          color: #000000;
        }

        .num {
          font-size: 40px;
          color: #eb0029;
        }
      }
    }

    .serve_item {
      margin-top: 30px;
      margin-bottom: 25px;

      .serve_title {
        margin-left: 20px;
        font-weight: bold;
      }

      .serve_info {
        font-size: 40px;
        color: #000000;

        &_price {
          margin-right: 15px;
        }
      }

      /deep/ .van-cell {
        background: transparent;
        padding: 15px 0;
      }
    }
  }

  .vip {
    padding: 10px 30px 30px 30px;
    margin-top: 30px;
    border-radius: 30px;
    background: linear-gradient(to right, #293a70, #575791);

    &_top {
      display: flex;
      align-items: center;
      justify-content: space-between;

      &_left {
        display: flex;
        align-items: center;

        .vip_icon {
          width: 98px;
          height: 89px;
        }

        .vip_tips {
          font-size: 38px;
          color: #fadab0;
          margin-left: 15px;
        }
      }

      &_right {
        display: flex;
        align-items: center;

        .btn {
          padding: 0 30px;
          height: 80px;
          background: #d49a50;
          color: #fff;
          line-height: 80px;
          border-radius: 999px;
          font-size: 44px;
        }

        .price {
          color: #fff;
          font-size: 46px;
          margin-left: 10px;
        }
      }
    }

    &_tip {
      padding: 15px 10px;
      background: #fff;
      margin-top: 20px;

      &_text {
        font-size: 38px;
        color: #000;

        &_price {
          font-size: 44px;
          color: #eb0029;
        }
      }
    }
  }

  .vip_rule {
    font-size: 35px;
    color: #333;
    text-align: center;
    margin-top: 25px;

    .rule_icon {
      font-size: 40px;
    }
  }

  .order_bottom {
    margin-top: 100px;
    box-sizing: border-box;
    // background: #fff;
    width: 100%;
    display: flex;
    justify-content: center;

    .pay_btn {
      height: 100px;
      line-height: 100px;
      border-radius: 50px;
      background: #e73353;
      color: #fff;
      text-align: center;
      font-size: 46px;
      padding: 0 120px;
    }
  }

  .red {
    color: #eb0029 !important;
  }

  .margin_left {
    margin-left: 30px;
  }

  /deep/ .van-cell__right-icon {
    display: flex;
    align-items: center;
  }
}

.button {
  width: 70%;
  font-size: 50px;
  color: white;
  background-color: #3985f7;
  padding: 20px 0;
  margin: 0 auto;
  text-align: center;
  border-radius: 64px;
}
</style>
